package com.hd.crdao.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.GetMobileInfoUtils;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/7/11 0011
 */
public class CommentActivity extends BaseSecondActivity {
    @BindView(R.id.comment_head)
    ExpandHeadView commentHead;
    @BindView(R.id.reason)
    EditText reason;
    @BindView(R.id.wordnumber)
    TextView wordnumber;

    @Override
    protected int setContentView() {
        return R.layout.activity_comment;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return commentHead;
    }

    @Override
    protected void initView() {
        reason.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // wordnumber.setText("0/15");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String word = reason.getText().toString().trim();
                wordnumber.setText(word.length() + "/15");


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        commentHead.getHeadViewRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String remarks = reason.getText().toString().trim();
                if (TextUtils.isEmpty(remarks)) {
                    MyToast.show("请填写理由后再上传");
                    return;
                }
                UploadRemarks(remarks);
            }
        });

    }

    /**
     * 上传备注说明到服务器
     *
     * @param remarks
     */
    private void UploadRemarks(String remarks) {
        HashMap<String, String> map = new HashMap<>();
        String userId = SPUtils.getString("userid", "");
        String vehicleId = SPUtils.getString("vehicleId","");
        String latLon =SPUtils.getString("latLon","");
        String cmmii = GetMobileInfoUtils.getDeviceId(this);
        map.put("sign", Constants.SIGN);
        map.put("userId", userId);
        map.put("vehicleId", vehicleId);
        map.put("latlon", latLon);
        map.put("cmmii", cmmii);
        map.put("remarks", remarks);

        OkHttpUtils.post().url(Constants.CARCHECKREMARKS_URL).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                if (msgBean.getSuccess() == 0) {
                    MyToast.show("上传成功");

                    startActivity(new Intent(UIUtils.getContext(),TodayCarCheckActivity.class));
                    onBackPressed();
                } else {
                    MyToast.show(msgBean.getMessage());
                }
            }
        });
    }


}
